package server.content.skills.fishing;

import core.util.Misc;
 
import server.content.skilling.PlayerSkill;
import server.event.CycleEvent;
import server.event.CycleEventContainer;
import server.event.CycleEventHandler;
import server.game.players.Client;

public class MFlyFishing extends PlayerSkill {

	private final int TROUT = 335;
	private final int SALMON = 331;

	public MFlyFishing(Client c, int skillId) {
		super(c, skillId);
		setTick(2);
	}

	@Override
	public void init() {
		kill();
		if (getClient().getItems().freeSlots() < 1) {
			kill();
			getClient().sendMessage(
					"Your inventory is too full to catch anymore fish.");
			return;
		}
		if (getPlayerLevel() <= 19) {
			kill();
			getClient().sendMessage(
					"You need a fishing level of atleast 20 to fish here.");
			return;
		}
		if (getClient().getItems().playerHasItem(309)
				&& getClient().getItems().playerHasItem(314)) {
			if (startTime < 0)
				startTime = System.currentTimeMillis();
			doAnimation(622);
			setActive(true);
			process();
		} else {
			getClient().sendMessage(
					"You need a Fly Fishing Rod and Feathers to fish here.");
			kill();
			return;
		}
	}

	@Override
	public void process() {
		CycleEventHandler.getSingleton().addEvent(getClient(),
				new CycleEvent() {

					@Override
					public void execute(CycleEventContainer container) {
						if (isActive()) {
							if (getClient().getItems().freeSlots() < 1) {
								kill();
								getClient()
										.sendMessage(
												"Your inventory is too full to catch anymore fish.");
								return;
							}
							if (!getClient().getItems().playerHasItem(314)) {
								kill();
								getClient().sendMessage(
										"You have run out of Feathers.");
								return;
							}
							doAnimation(622);
							if (getPlayerLevel() >= 30) {
								if (Misc.random(100) <= getChanceForLevel()) {
									final int randFish = Misc.random(8);
									if (randFish == 1 || randFish == 5
											|| randFish == 8) {
										addItem(SALMON, 1);
										addXP(65, getSkillId());
										getClient()
												.sendMessage(
														"You manage to catch a Salmon.");
										deleteItem(314, 1);
									} else {
										addItem(TROUT, 1);
										addXP(45, getSkillId());
										getClient().sendMessage(
												"You manage to catch a Trout.");
										deleteItem(314, Misc.random(3));
									}
								}
							} else if (getPlayerLevel() <= 29) {
								if (Misc.random(100) <= getChanceForLevel()) {
									addItem(TROUT, 1);
									addXP(45, getSkillId());
									getClient().sendMessage(
											"You manage to catch a Trout.");
									deleteItem(314, Misc.random(3));
								}
							}
						} else {
							container.stop();
						}
					}

					@Override
					public void stop() {

					}
				}, getTick());
	}

	@Override
	public void kill() {
		setTick(2);
		setActive(false);
		resetAnim();
	}

	public int getChanceForLevel() {
		final int lvl = getPlayerLevel();
		if (lvl > 19 && lvl < 39) {
			return 28;
		} else if (lvl > 39 && lvl < 51) {
			return 35;
		} else if (lvl > 50 && lvl < 61) {
			return 42;
		} else if (lvl > 60 && lvl < 71) {
			return 46;
		} else if (lvl > 70 && lvl < 81) {
			return 52;
		} else if (lvl > 80 && lvl < 99) {
			return 56;
		}
		return 10;
	}

}
